package com.woan.api.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class CopyFile {
	public static String  MoveFile(String oldPath,String newPath,String oldName){
		
		File oldFile=new File(oldPath);
	
		String newName="";
		try {
			newName=oldName.substring(oldName.indexOf("_")+1);
			File nFile=new File(newPath+"/"+newName);
			if(nFile.exists()){
				nFile.delete();
			}
			FileInputStream input=new FileInputStream(oldFile);
			FileOutputStream output=new FileOutputStream(newPath+"/"+newName);
			byte[] b=new byte[1024*5];
			int len;
			while ((len=input.read(b))!=-1) {
				output.write(b,0,len);
			}
			output.flush();
			output.close();
			input.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return newName;
	}
	
	
	public static void CopyFile2(File file,String newPath){
		try {
			if(!file.exists()){
				FileInputStream input=new FileInputStream(file);
				String fname = file.getName();
				FileOutputStream output=new FileOutputStream(newPath+"/"+fname);
				byte[] b=new byte[1024*5];
				int len;
				while ((len=input.read(b))!=-1) {
					output.write(b,0,len);
				}
				output.flush();
				output.close();
				input.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public static void CopyPath(String oldPath,String newPath){
	
		  File f=new File(newPath);
		  if(!f.exists()){
			 f.mkdirs();
		  }
	
		  File oldFile=new File(oldPath);
		  String[] file=oldFile.list();
		  File temp=null;
		  //System.out.println(file.length);
		  for(int i=0;i<file.length;i++){
			  if(oldPath.endsWith(File.separator)){
				  //System.out.println(oldPath+file[i]);
				  temp=new File(oldPath+file[i]);
			  }else{
				  //System.out.println(oldPath+File.separator+file[i]);
				  temp=new File(oldPath+File.separator+file[i]);
			  }
			  if(temp.isFile()){
				 try {
					File file2 = new File(newPath+"/"+(temp.getName()).toString());
					if(!file2.exists()){
						FileInputStream input=new FileInputStream(temp);
						String fname = temp.getName();
						if(fname.contains(".") && fname.contains("_")){
							fname = temp.getName().substring(file[i].indexOf("_")+1);
						  }
						FileOutputStream output=new FileOutputStream(newPath+"/"+(fname).toString());
						byte[] b=new byte[1024*5];
						int len;
						while ((len=input.read(b))!=-1) {
							output.write(b,0,len);
						}
						output.flush();
						output.close();
						input.close();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				  
			  }
			  if(temp.isDirectory()){
				  CopyPath(oldPath+"/"+file[i],newPath+"/"+file[i]);
			  }
		  }
	  }
	
	public static void CopyFile2(String oldPath,String newPath){
		  
		  File f=new File(newPath);
		  if(!f.exists()){
			 f.mkdirs();
		  }
		
		  File oldFile=new File(oldPath);
		  String[] file=oldFile.list();
		  File temp=null;
		  //System.out.println(file.length);
		  for(int i=0;i<file.length;i++){
			  if(oldPath.endsWith(File.separator)){
				  //System.out.println(oldPath+file[i]);
				  temp=new File(oldPath+file[i]);
			  }else{
				 // System.out.println(oldPath+File.separator+file[i]);
				  temp=new File(oldPath+File.separator+file[i]);
			  }
			  if(temp.isFile()){
				 try {
					File file2 = new File(newPath+"/"+(temp.getName()).toString());
					if(!file2.exists()){
						FileInputStream input=new FileInputStream(temp);
						String fname = temp.getName();
						FileOutputStream output=new FileOutputStream(newPath+"/"+(fname).toString());
						byte[] b=new byte[1024*5];
						int len;
						while ((len=input.read(b))!=-1) {
							output.write(b,0,len);
						}
						output.flush();
						output.close();
						input.close();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				  
			  }
			  if(temp.isDirectory()){
				  CopyFile2(oldPath+"/"+file[i],newPath+"/"+file[i]);
			  }
		  }
	  }
	 
	public static void CopyFile(String oldPath, String newPath,String imgName) throws Exception {
	
		File f = new File(newPath);
		if (!f.exists()) {
			f.mkdirs();
		}
		
		File temp = new File(oldPath);
		if (temp.isFile()) {
			try {
				File file2 = new File(newPath + "/"
						+ imgName);
				if (!file2.exists()) {
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath
							+ "/" + imgName);
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
	public static void doFolder(String folderPath) {
		doAllFile(folderPath);
		String filePath = folderPath;
		filePath = filePath.toString();
		File file = new File(filePath);
		file.delete();
	}

	
	public static void doAllFile(String path) {
		File file = new File(path);
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				doAllFile(path + "/" + tempList[i]);
				doFolder(path + "/" + tempList[i]);
			}
		}
		if (file.exists()) {
			file.delete();
		}
	}
	
	public static void extZipFileList(String zipFileName, String extPlace) {
		try {

			ZipInputStream in = new ZipInputStream(new FileInputStream(
					zipFileName));

			ZipEntry entry = null;

			while ((entry = in.getNextEntry()) != null) {

				String entryName = entry.getName();

				if (entry.isDirectory()) {
					File file = new File(extPlace + entryName);
					file.mkdirs();
					//System.out.println("1111111111111111111111: " + entryName);
				} else {

					FileOutputStream os = new FileOutputStream(extPlace
							+ entryName);

					// Transfer bytes from the ZIP file to the output file
					byte[] buf = new byte[1024];

					int len;
					while ((len = in.read(buf)) > 0) {
						os.write(buf, 0, len);
					}
					os.close();
					in.closeEntry();

				}
			}

		} catch (IOException e) {
		}
		//System.out.println("OK ");
	}
	
	
	public static String[] doFile(String path) {
		File file = new File(path);
		String[] tempList = file.list();
		return tempList;
	}
	
	
	public static String Random() {
		Random rd = new Random();
		String id = "1";
		for (int i = 0; i < 10; i++) {
			id = (int) (rd.nextFloat() * 10000) + "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date curDate = new Date();
		id += formatter.format(curDate);
		return id;

	}
	
	
	public static String Random2() {
		Random rd = new Random();
		String pwd = "";
		for(int i=0;i<6;i++){ 
			   pwd+=""+rd.nextInt(10); 
	    } 
        return pwd;
	}
	public static void main(String[] args) {
		Random rd = new Random();
		String pwd = "";
		for(int i=0;i<6;i++){ 
			 pwd+=""+rd.nextInt(10); 
					 // System.out.println(pwd);
	    } 
		//System.out.println(pwd);
	}
}
